﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Collections;
using System.Xml.Linq;

namespace Sofire.Data.XSql
{
    internal class XSqlIncludeNode : XSqlNodeBase, IXSqlIncludeNode
    {
        public const string NodeName = "include";

        public override XSqlType Type
        {
            get { return XSqlType.Include; }
        }

        private string _Owner;
        public string Owner
        {
            get
            {
                return this._Owner;
            }
            set
            {
                this._Owner = value;
            }
        }

        private string _Path;
        public string Path
        {
            get
            {
                return this._Path;
            }
            set
            {
                this._Path = value;
            }
        }

        public override XObject CreateXObject()
        {
            XElement element = new XElement(NodeName);
            element.SetAttributeValue(XSqlAttributes.Path, this._Path);
            element.SetAttributeValue(XSqlAttributes.Owner, this._Owner);
            //this.CreateXChildren(element);
            return element;
        }
        protected override void Load(XmlNode node)
        {
            this._Path = this.GetAttribute(node, XSqlAttributes.Path);
            this._Owner = this.TryGetAttribute(node, XSqlAttributes.Owner);
        }

        public override string Compile(XSqlParameterCollection parameters)
        {
            XSqlDocument document = this._Owner == null ? this.Document : XSqlManager.Find(this._Owner) as XSqlDocument;
            if(document == null) this.ThrowException(XSqlError.UnfoundDocument, this._Path);
            var node = document.XSqls[this._Path];
            if(node == null) this.ThrowException(XSqlError.NoExists, this._Path);
            return node.Compile(parameters);
        }
    }
}
